package com.itheima.controller;

import com.itheima.R;
import com.itheima.pojo.SmsFlashPromotionProductRelation;
import com.itheima.service.FlashProductRelationService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Hakuna Matata
 * @date 2022-10-12-20:54
 */
@RestController
@RequestMapping("flashProductRelation")
public class FlashProductRelationController {

    @Autowired
    private FlashProductRelationService flashProductRelationService;

    //批量选择商品添加关联
    @PostMapping("create")
    public R flashProductRelationCreate(@RequestBody SmsFlashPromotionProductRelation relationList) {
        flashProductRelationService.create(relationList);
        return R.success("批量选择商品添加关联成功!");
    }

    //删除关联
    @PostMapping("delete/{id}")
    public R flashProductRelationDelete(@PathVariable("id") Long id) {
        flashProductRelationService.delete(id);
        return R.success("删除成功!");
    }

    //分页查询不同场次关联及商品信息
    @GetMapping("list")
    public R productionRelationListBySessionByPage(Integer flashPromotionId,
                                                   Integer flashPromotionSessionId,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        PageResult pageResult = flashProductRelationService.list(flashPromotionId, flashPromotionSessionId, pageNum, pageSize);
        return R.success(pageResult);
    }

    /**
     * 修改关联信息
     *
     * @param id
     * @param flashPromotionProductRelation
     * @return
     */
    @PostMapping("update/{id}")
    public R productionRelationUpdate(@PathVariable("id") Long id, @RequestBody SmsFlashPromotionProductRelation flashPromotionProductRelation) {

        flashProductRelationService.update(id, flashPromotionProductRelation);
        return R.success("修改成功!");
    }

    //获取管理商品促销信息
    @GetMapping("{id}")
    public R getFlashProductRelationInfo(@PathVariable("id") Long id) {
        SmsFlashPromotionProductRelation relation = flashProductRelationService.getRelationInfo(id);
        return R.success(relation);
    }

}
